package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {

    @Autowired
    private EmpExprService empExprService;

    @Autowired
    private DeptMapper deptMapper;

    /**
     * 保存员工信息
     * @param emp
     * @return
     */
    @Override
    @Transactional
    public boolean saveEmpAndExprList(Emp emp) {
        this.save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(v -> v.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
        return true;
    }

    /**
     * 分页条件查询
     * @param param
     * @return
     */
    @Override
    public Result getPageREsult(EmpQueryParam param) {
        Page<Emp> page = Page.of(param.getPage(),param.getPageSize());
        String name = param.getName();
        Integer gender = param.getGender();
        LocalDate begin = param.getBegin();
        LocalDate end = param.getEnd();
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<Emp>()
                .like(StringUtils.isNotBlank(name), Emp::getName, name)
                .eq(ObjectUtils.isNotNull(gender),Emp::getGender, gender)
                .between(ObjectUtils.isNotNull(begin) && ObjectUtils.isNotNull(end), Emp::getEntryDate, begin, end);
        this.page(page,queryWrapper);
        List<Emp> emps = page.getRecords();
        Set<Integer> deptIds = emps.stream().map(Emp::getDeptId).collect(Collectors.toSet());
        List<Dept> depts = deptMapper.selectList(new LambdaQueryWrapper<Dept>().in(Dept::getId, deptIds));
        Map<Integer, String> deptMap = depts.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
        emps.forEach(v -> v.setDeptName(deptMap.get(v.getDeptId())));
        PageResult<Emp> pageResult = new PageResult<>(page.getTotal(),emps);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = getById(id);
        // 工作经历列表
        emp.setExprList(empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id)));
        return emp;
    }

    /**
     * 修改员工
     * @param emp
     */
    @Override
    @Transactional
    public void updateEmp(Emp emp) {

        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);

        // 根据id批量删除员工的工作经历信息
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));

        // 批量保存员工的工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && !exprList.isEmpty()) {
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
        }
        empExprService.saveBatch(exprList);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void removeEmpsByIds(List<Integer> ids) {
        //删除员工
        removeBatchByIds(ids);
        //删除工作经历
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));
    }
}
